package com.sls.sys.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.sls.page.Pagination;
import com.sls.sys.dao.OperatorDAO;
import com.sls.sys.pojo.OperatorInfo;
import com.sls.sys.pojo.OperatorInfoVO;
import com.sls.sys.pojo.PermissionInfo;
import com.sls.sys.pojo.RoleInfo;
import com.sls.sys.querybean.OperatorQuery;
import com.sls.sys.querybean.RoleInfoQuery;

@Repository("operatorDAO")
public class OperatorDAOImpl extends SuperDAO<OperatorInfoVO> implements OperatorDAO {

	@Override
	public void insertOperator(OperatorInfo operator) {
		getSqlSession().insert("mybatis.xml.OperatorInfoMapper.insertToOperatorInfo",
				operator);
	}

	

	@Override
	public void deleteOperByAccount(String account) {
		getSqlSession().delete(
				"mybatis.xml.OperatorInfoMapper.deleteOperatorInfoByAccount", account);

	}

	@Override
	public void updateOperById(OperatorInfoVO operator) {
		getSqlSession().update(
				"mybatis.xml.OperatorInfoMapper.updateOperatorInfoById", operator);

	}

	@Override
	public void updateOperLogin(OperatorInfoVO operator) {
		getSqlSession().update("mybatis.xml.OperatorInfoMapper.updateOperatorInfoLogin",
				operator);

	}

	@Override
	public OperatorInfoVO queryAllByAccount(String account) {
		OperatorInfoVO o = getSqlSession().selectOne(
				"mybatis.xml.OperatorInfoMapper.queryAllByAccount", account);
		return o;
	}

	@Override
	public Pagination<OperatorInfoVO> queryOperator(OperatorQuery query) {
		return this.queryPagination("mybatis.xml.OperatorInfoMapper.queryOperatorsPage",
				"mybatis.xml.OperatorInfoMapper.queryOperatorsNumber", query);
	}

	// add by sxf
	public Pagination<RoleInfo> queryRole(RoleInfoQuery query) {
		query.calculate();
		List<RoleInfo> list = getSqlSession().selectList(
				"mybatis.xml.OperatorInfoMapper.queryRolePage", query);
		int rowCount = getSqlSession().selectOne(
				"mybatis.xml.OperatorInfoMapper.queryRoleNumber", query);
		Pagination<RoleInfo> page = new Pagination<RoleInfo>(
				query.getCurrentPage(), rowCount, query.getPageSize(),
				list);
		return page;
	}	

	@Override
	public List<String> queryAllCodeByAccount(String account) {
		List<PermissionInfo> list = getSqlSession().selectList(
				"mybatis.xml.PermissionInfoMapper.queryAllCodeByAccount", account);
		List<String> reList = new ArrayList<String>();
		if (list != null) {
			for (PermissionInfo p : list) {
				reList.add(p.getCode());
			}
		}
		return reList;
	}

	@Override
	public void updatePwdByAccount(OperatorInfoVO operator) {
		getSqlSession().update("mybatis.xml.OperatorInfoMapper.updatePwdByAccount",
				operator);
	}


	@Override
	public OperatorInfoVO queryAllById(String id) {

		return getSqlSession().selectOne("mybatis.xml.OperatorInfoMapper.queryAllById",
				id);
	}

	@Override
	public void update_lockOperator(OperatorInfo info) {
		getSqlSession().update("mybatis.xml.OperatorInfoMapper.lockOperator",
				info);
	}
}
